//
//  FXStatusToolBar.swift
//  微博(Swift)
//
//  Created by V on 16/1/3.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit

class FXStatusToolBar: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusViewModel: FXStatusViewModel?{
        didSet{
            // 当给 FXStatusCell 赋值的时候，会调用 FXStatusCell 的 didSet 方法，从而调用到这个方法
            // 在这里给 toolBar 中的按钮设置相对应的文字
            /**
                        如果使用前三行代码，出现的问题：1. cell 的toolBar 加载出来是正常显示评论转发数量的，当点击后，就会变成默认文字
                        再次点击也不会变回正常的数字
            */
            //            retweetBtn.titleLabel?.text = statusViewModel?.repostsCount
            //            commentBtn.titleLabel?.text = statusViewModel?.commentsCount
            //            attitudeBtn.titleLabel?.text = statusViewModel?.attitudesCount
            retweetBtn.setTitle(statusViewModel?.repostsCount, forState: UIControlState.Normal)
            commentBtn.setTitle(statusViewModel?.commentsCount, forState: UIControlState.Normal)
            attitudeBtn.setTitle(statusViewModel?.attitudesCount, forState: UIControlState.Normal)
        }
    }
    
    
    
    
    
    
    
    
    // MARK: - 懒加载控件
    lazy var retweetBtn: UIButton = UIButton()
    lazy var commentBtn: UIButton = UIButton()
    lazy var attitudeBtn: UIButton = UIButton()
    
    
    
    
    
    private func setupUI(){
        //        backgroundColor = RandomColor()
        // 添加三个按钮
        retweetBtn = addChildButton("转发", imgName: "timeline_icon_retweet")
        commentBtn = addChildButton("评论", imgName: "timeline_icon_comment")
        attitudeBtn = addChildButton("赞", imgName: "timeline_icon_unlike")
        
        let sp1 = addSpliteView()
        let sp2 = addSpliteView()
        
        // 转发按钮
        retweetBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentBtn.snp_width)
        }
        
        // 评论按钮
        commentBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetBtn.snp_trailing)
            make.top.equalTo(retweetBtn)
            make.bottom.equalTo(retweetBtn)
            make.width.equalTo(attitudeBtn.snp_width)
        }
        // 评论
        attitudeBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(commentBtn.snp_trailing)
            make.trailing.equalTo(self)
            make.top.equalTo(commentBtn)
            make.bottom.equalTo(commentBtn)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetBtn.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentBtn.snp_trailing)
            make.centerY.equalTo(self)
        }
    }
    
    
    
    // MARK: - 本类中添加 子控件 需要用到的方法
    // 添加子按钮
    private func addChildButton(title: String, imgName: String) ->UIButton{
        
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSize: 14)
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        // 设置标题
        button.setTitle(title, forState: UIControlState.Normal)
        addSubview(button)
        return button
    }
    
    // 添加分割线
    private func addSpliteView() ->UIView{
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
}



